import 'dart:math';

import 'package:flutter/material.dart';

class BuildingList extends StatefulWidget {
  const BuildingList({super.key, required this.point});

  final String point;

  @override
  State<BuildingList> createState() => _RoomListState();
}

class _RoomListState extends State<BuildingList> {
  Map data = {
    "point": "", // 记录小区信息
    "size": 0, // 随机产生楼栋的数量
    "type": "", // 小区的楼栋名称(size>4 单元, size<=4 号楼)
  };

  @override
  void initState() {
    super.initState();
    // 传入小区，获取小区楼栋信息
    fakeData(widget.point);
  }

  fakeData(point) {
    // 1.1 随机产生楼栋的数量
    Random random = Random();
    // 随机产生楼栋的数量
    int size = random.nextInt(6) + 2; // 2-7
    // 1.2 确定小区楼栋的名称
    String type = size > 5 ? '单元' : '号楼';
    // 1.3 更新数据
    setState(() {
      data = {
        "point": widget.point,
        "size": size,
        "type": type,
      };
      print(data);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('选择楼栋'),
      ),
      body: ListView.builder(
        itemBuilder: (BuildContext context, int index) {
          // 小区信息
          final point = data['point'];
          // 楼栋信息
          final building = '${index + 1}${data['type']}';
          return Container(
            color: Colors.white,
            padding: const EdgeInsets.all(10),
            child: GestureDetector(
              onTap: () {
                Navigator.pushNamed(
                  context,
                  // 分步表单第二步: 跳转到房间选择页
                  '/room_list',
                  // 传递小区信息+楼栋信息
                  arguments: {
                    'point': point,
                    'building': building,
                  },
                );
              },
              child: Row(
                children: [
                  // 小区信息 + 楼栋信息
                  Expanded(child: Text('$point $building')),
                  const Row(
                    children: [
                      Icon(Icons.arrow_forward_ios,
                          size: 16, color: Colors.black),
                    ],
                  )
                ],
              ),
            ),
          );
        },
        itemCount: data['size'],
      ),
    );
  }
}
